package com.module.overview.ui.summaryReport

import com.common.app.data.bean.overView.SummaryReportRecordBean
import com.common.app.data.bean.overView.SummaryReportRecordListModel
import com.common.app.utls.TimeUtils
import com.common.base.app.fragment.BaseVMFragment
import com.common.base.utils.Page
import com.common.base.widget.animator.CustomAnimation1
import com.module.overview.R
import com.module.overview.databinding.OverviewFragmentSubSummaryReportRecordBinding
import com.scwang.smart.refresh.header.ClassicsHeader
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import kotlin.random.Random

/**
 *
 * 作者：sosou
 *
 * 版本：1.0
 *
 * 创建日期：2025/07/22
 *
 * 描述：
 *
 * 修订历史：
 *
 */
class SummaryReportRecordFragment :
    BaseVMFragment<OverviewFragmentSubSummaryReportRecordBinding, SummaryReportMainViewModel>() {

    companion object {
        fun newInstance(mType: Int): SummaryReportRecordFragment {
            val fragment = SummaryReportRecordFragment()
            fragment.mType = mType
            return fragment
        }

    }

    private var mType = 0

    private var page: Page = Page()


    private val mAdapter by lazy { SummaryReportRecordListAdapter() }

    override fun getPlaceholderView() = mViewBinding.placeHolderView

    override fun getViewModel() = SummaryReportMainViewModel::class.java

    override fun getViewBinding() =
        OverviewFragmentSubSummaryReportRecordBinding.inflate(layoutInflater)

    override fun initViews() {
        super.initViews()

        mViewBinding.hrv.setItemViewCacheSize(30)
        getRefreshFooter()?.let { mViewBinding.pdView.setRefreshFooter(it) }

//        mViewBinding.pdView.setEnableFooterFollowWhenNoMoreData(true)
        mViewBinding.pdView.setEnableLoadMore(true)
//        mViewBinding.pdView.setEnableLoadMoreWhenContentNotFull(false)
        mViewBinding.pdView.setRefreshHeader(ClassicsHeader(requireActivity()))

        mAdapter.apply {
            adapterAnimation = CustomAnimation1()
            isAnimationFirstOnly = true
            addFooterView(layoutInflater.inflate(R.layout.base_rlv_margin_footer_tr_view50, null))
            mViewBinding.hrv.adapter = this
        }
    }

    override fun lazyInitData() {
        super.lazyInitData()
        showEmpty(getString(R.string.common_not_report_hint))
        initData()
    }

    override fun initEvents() {
        super.initEvents()
        setErrorRetryListener {
            lazyInitData()
        }

        mViewBinding.pdView.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                initData()
                refreshLayout.layout.postDelayed({
                    finishPdView()
                }, 1000)
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                finishPdView()
            }
        })
    }

    private fun initData() {
        page.reset()

        val mList = mutableListOf<SummaryReportRecordListModel>()
        repeat(Random.nextInt(2, 9)) { index ->
            val tempList = mutableListOf<SummaryReportRecordBean>()
            repeat(Random.nextInt(3, 6)) { index ->
                tempList.add(SummaryReportRecordBean(startTime = TimeUtils.getTimeDateLong(), endTime = TimeUtils.getTimeDateLong(), tips = if (Random.nextBoolean()) "asasa" else null))
            }
            mList.add(SummaryReportRecordListModel(time = TimeUtils.getTimeDateLong(), mData = tempList))
        }
        mAdapter.setList(mList)
        showContent()
    }

    private fun finishPdView() {
        mViewBinding.pdView.finishRefresh()
        mViewBinding.pdView.finishLoadMore()
    }
}